package com.kakaxi.mybilibili.module.home.live.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.LiveConsiderRecyAdapter;
import com.kakaxi.mybilibili.base.NavigationActivity;
import com.kakaxi.mybilibili.bean.LiveConsiderItemBean;
import com.kakaxi.mybilibili.module.home.live.presenter.ILiveConsiderPresenter;
import com.kakaxi.mybilibili.module.home.live.presenter.LiveConsiderPresenter;
import com.kakaxi.mybilibili.utils.NetworkCheckUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 直播关注主播活动类
 * Created by kakaxi on 16/11/13.
 */

public class LiveConsiderActivity extends NavigationActivity implements ILiveConsiderView {

    @BindView(R.id.recv_common_fragment)
    RecyclerView mLiveConsiderRecyView;

    @BindView(R.id.srl_common_fragment)
    SwipeRefreshLayout mLiveConsiderSRLayout;

    /** 关注主播列表适配器 **/
    private LiveConsiderRecyAdapter mLiveConsiderAdapter;

    /** 控制层 **/
    private ILiveConsiderPresenter mPresenter;

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_live_consider;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        showBackBtn();

        // 列表初始化
        mLiveConsiderRecyView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                // 跳转到对应主播的直播间
                LiveConsiderItemBean liveConsiderItemBean = mLiveConsiderAdapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putInt(LiveRoomActivity.EXTRA_ROOM_ID, liveConsiderItemBean.getRoommid());
                //bundle.putString(LiveRoomActivity.EXTRA_PLAY_URL, liveConsiderItemBean.get);
                goActivity(LiveRoomActivity.class, bundle);
            }
        });
        // 刷新控件初始化
        mLiveConsiderSRLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refreshLiveConsiderData();
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter = new LiveConsiderPresenter(this);
        mPresenter.initLiveConsiderData();
    }

    @Override
    public void showLiveConsiderData(List<LiveConsiderItemBean> liveConsiderItemBeanList) {
        if (liveConsiderItemBeanList != null) {
            if (mLiveConsiderAdapter == null) {
                mLiveConsiderAdapter = new LiveConsiderRecyAdapter(liveConsiderItemBeanList);
                mLiveConsiderRecyView.setAdapter(mLiveConsiderAdapter);
            } else {
                mLiveConsiderAdapter.setNewData(liveConsiderItemBeanList);
            }
        }
    }

    @Override
    public boolean isConnectNetwork() {
        return NetworkCheckUtil.isNetworkConnected(getBaseContext());
    }

    @Override
    public int getConnectType() {
        return NetworkCheckUtil.getConnectedType(getBaseContext());
    }

    @Override
    public void showLoading() {
        mLiveConsiderSRLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mLiveConsiderSRLayout.setRefreshing(false);
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {
        toastHandler(message);
    }
}
